package com.ljq.consumers;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.Optional;


/**
 * Kafka消息接收端-支持多端消费(随机消费)
 *
 * @author liujiaqi
 * @date 2023/02/14
 */
@Component
@Slf4j
public class KafkaConsumer {


    @KafkaListener(topics = "topic.test", groupId = "topic.group1")
    public void topicTestA(ConsumerRecord<?, ?> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        Optional message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object msg = message.get();
            log.info("客户端 A 消费了： Topic[{}] Message[{}]", topic, msg);
            ack.acknowledge();
        }
    }

    @KafkaListener(topics = "topic.test", groupId = "topic.group1")
    public void topicTestB(ConsumerRecord<?, ?> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        Optional message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object msg = message.get();
            log.info("客户端 B 消费了： Topic[{}] Message[{}]", topic, msg);
            ack.acknowledge();
        }
    }


    @KafkaListener(topics = "topic.test", groupId = "topic.group1")
    public void topicTestC(ConsumerRecord<?, ?> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        Optional message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object msg = message.get();
            log.info("客户端 C 消费了： Topic[{}] Message[{}]", topic, msg);
            ack.acknowledge();
        }
    }
}
